home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFcolours - Star Fighter 3000 colours editor
- * Main application skeleton
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <signal.h>
-
- /* RISC OS library files */
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "window.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "hourglass.h"
- #include "Macros.h"
- #include "Loader.h"
- #include "Pal256.h"
- #include "ViewsMenu.h"
- #include "InputFocus.h"
-
- /* Local headers */
- #include "SFCIconbar.h"
- #include "SFCSaveBox.h"
- #include "SFCFileInfo.h"
- #include "Menus.h"
- #include "SFCCreateFile.h"
- #include "PreQuit.h"
- #include "DCS_dialogue.h"
- #include "Main.h"
- #include "Utils.h"
-
- #define WimpVersion 310
-
- /* Our toolbox events */
- #define EVENT_HELPFILE 0x03
- #define EVENT_QUIT 0x09
-
- _kernel_oserror shared_err_block = {255, ""};
- int palette[256];
- ObjectId pal256_sharedid = NULL_ObjectId;
- char taskname[32];
- #ifdef INT_COLOUR_FIND
- bool use_colour_trans = false;
- #endif
-
- static WimpPollBlock poll_block;
- static IdBlock id_block;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler tb_quithandler, showhelp_handler, autocreate_handler, error_handler;
- static WimpMessageHandler PreQuit_handler, wimpquit_handler;
- static void initialise(void);
- #ifdef INT_COLOUR_FIND
- static void process_arguments(int argc, char *argv[]);
- #endif
- static void simple_exit(_kernel_oserror *e);
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- int main(int argc, char *argv[])
- {
- /* CLI parameters are ignored, as we claim no filetypes */
- int event_code;
-
- hourglass_on();
- #ifdef INT_COLOUR_FIND
- process_arguments(argc, argv);
- #endif
- initialise();
- hourglass_off();
-
- /*
- * poll loop
- */
- while (TRUE)
- event_poll (&event_code, &poll_block, 0);
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int tb_quithandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Quit entry on iconbar menu */
- if(TRY_QUIT(0))
- exit(EXIT_SUCCESS);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int PreQuit_handler(WimpMessage *message, void *handle)
- {
- int th;
- if(message->hdr.size < 20 || message->data.words[0] == 0)
- th = message->hdr.sender; /* shutdown in progress */
- else
- th = 0; /* just our task */
-
- /* If function returns false then unsaved data */
- if(!TRY_QUIT(th)) {
-
- /* Object by acknowledging message */
- message->hdr.your_ref = message->hdr.my_ref;
- RE(wimp_send_message(Wimp_EUserMessageAcknowledge, message, message->hdr.sender, NULL, NULL))
- }
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int wimpquit_handler(WimpMessage *message,void *handle)
- {
- /* Quit application immediately */
- exit(EXIT_SUCCESS);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int showhelp_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Show application help file */
- if(_kernel_oscli("Filer_Run <SFcolours$Dir>.!Help") == _kernel_ERROR)
- err_check_rep(_kernel_last_oserror());
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int autocreate_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Catch auto-created objects and initialise handlers etc. */
- ToolboxObjectAutoCreatedEvent *toace = (ToolboxObjectAutoCreatedEvent *)event;
- if (strcmp(toace->template_name, "Iconbar") == 0) {
- Iconbar_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "CreateFile") == 0) {
- CreateFile_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "Menu") == 0) {
- EF(ViewsMenu_parentcreated(id_block->self_id, 0x03));
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "PreQuit") == 0) {
- PreQuit_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "ColsMenu") == 0) {
- ColsMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "FileMenu") == 0) {
- FileMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "EditMenu") == 0) {
- EditMenu_initialise(id_block->self_id);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "SaveFile") == 0) {
- SaveFile_initialise(id_block);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "FileInfo") == 0) {
- FileInfo_initialise(id_block);
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "Pal256") == 0) {
- EF(Pal256_initialise(id_block->self_id));
- EF(event_register_toolbox_handler(id_block->self_id, Window_AboutToBeShown, InputFocus_recordcaretpos, NULL));
- pal256_sharedid = id_block->self_id; /* we only have one palette */
- return 1; /* claim event */
- }
- if (strcmp(toace->template_name, "DCS") == 0) {
- DCS_initialise(id_block->self_id);
- return 1; /* claim event */
- }
-
- return 0; /* event not handled */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int error_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ToolboxErrorEvent *totee = (ToolboxErrorEvent *)event;
- if(totee->errnum == 0x80b633 || totee->errnum == 0x131c3) /* "To save drag...", locked file */
- err_report(totee->errnum, totee->errmess);
- else
- err_complain(totee->errnum, totee->errmess);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void initialise(void)
- {
- int toolbox_events = 0,
- wimp_messages = 0,
- wimpversion;
-
- /*
- * Prevent termination on SIGINT (we use the escape key ourselves)
- */
- signal(SIGINT, SIG_IGN);
-
- /*
- * register ourselves with the Toolbox.
- */
-
- {
- _kernel_oserror *e = toolbox_initialise (0, WimpVersion, &wimp_messages, &toolbox_events, "<SFcoloursRes$Dir>",msgs_get_descriptor(), &id_block, &wimpversion, 0, 0);
- if(e != NULL)
- simple_exit(e);
- }
-
- strncpy(taskname, msgs_lookup("_TaskName"), sizeof(taskname)-1);
- err_set_taskname(taskname, (wimpversion >= 321));
-
- /*
- * initialise the flex library
- */
-
- flex_init(taskname, (int *)msgs_get_descriptor(), 0); /* (use Wimpslot and own messages file) */
- flex_set_budge(1); /* allow budging of flex when heap extends */
-
- /*
- * initialise the event library.
- */
-
- event_initialise (&id_block);
- event_set_mask (Wimp_Poll_NullMask |
- Wimp_Poll_KeyPressedMask);
-
- EF(event_register_toolbox_handler(-1, Toolbox_ObjectAutoCreated, autocreate_handler, 0));
- EF(event_register_toolbox_handler(-1, Toolbox_Error, error_handler, 0));
- EF(event_register_toolbox_handler(-1, EVENT_HELPFILE, showhelp_handler, 0));
- EF(event_register_toolbox_handler(-1, EVENT_QUIT, tb_quithandler, 0));
- EF(event_register_message_handler(Wimp_MPreQuit, PreQuit_handler, 0));
- EF(event_register_message_handler(Wimp_MQuit, wimpquit_handler, 0));
- EF(InputFocus_initialise());
-
- /* Initialise Loader */
- EF(loader_initialise(0));
-
- /* Read the default mode 13 palette */
- {
- _kernel_swi_regs regs;
- regs.r[0] = 13; /* mode 13 */
- regs.r[1] = 0; /* read default palette */
- regs.r[2] = (int)palette; /* pointer to buffer */
- regs.r[3] = sizeof(palette); /* size of buffer */
- regs.r[4] = 0; /* flags */
- EF(_kernel_swi(ColourTrans_ReadPalette, ®s, ®s));
- }
-
- EF(ViewsMenu_create());
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void simple_exit(_kernel_oserror *e)
- {
- /* Limited amount we can do with no messages file... */
- wimp_report_error(e, Wimp_ReportError_Cancel, "SFcolours");
- exit(EXIT_FAILURE);
- }
-
- /* ----------------------------------------------------------------------- */
-
- #ifdef INT_COLOUR_FIND
- static void process_arguments(int argc, char *argv[])
- {
- /*
- * Look at command-line parameters
- */
-
- for(int i = 1; i < argc; i++) {
- if(ViewsMenu_strcmp_nc(argv[i], "-usect"))
- use_colour_trans = true;
- else {
- strcpy(shared_err_block.errmess, "Bad command line parameters");
- simple_exit(&shared_err_block);
- }
- }
- }
- #endif
-